package org.vectrics.recruiter.struts.tag;

import java.text.SimpleDateFormat;
import java.util.Enumeration;
import java.util.Iterator;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Category;
import org.vectrics.SystemException;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.html.TextHelper;
import org.vectrics.history.Activity;
import org.vectrics.history.HistoryProvider;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.struts.admin.SaveJobAction;
import org.vectrics.struts.VectricsTagSupport;

public class JobHistoryTag extends VectricsTagSupport {
    public static final long serialVersionUID = 972177364;

    static private Category log = Category.getInstance(JobHistoryTag.class);

    private static SimpleDateFormat historyDateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm a");

    private String jobId = null;

    private String width = "100%";

    public JobHistoryTag() {
        super();
    }

    public void setJobId(final String id) {
        jobId = id;
        log.debug("setJob() - set to: " + jobId);
    }

    public String getJobId() {
        return jobId;
    }

    public void parametersToLog(final String owner, final ServletRequest request) {
        Enumeration paramEnum = request.getParameterNames();
        while (paramEnum.hasMoreElements()) {
            String paramName = (String) paramEnum.nextElement();
            log.debug(owner + " - request param = " + request);
        }
    }

    @Override
    public String makeStartTag() {
        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        StringBuffer html = new StringBuffer();
        parametersToLog("doStartTag()", request);

        log.debug("doStartTag() called");

        log.debug("doStartTag() - getting http params");
        Job job = null;
        if (jobId == null || jobId.trim().length() <= 0) {
            log.debug("Job parameter was not specified, checking http session to see if current job registered");
            jobId = (String) request.getSession().getAttribute(SaveJobAction.CURRENT_JOB_KEY);
        }

        if (jobId == null)
            throw new SystemException("Job ID not specified");
        // log.error("doRender() - 'job' param not given, new job being edited.");
        // mainCell.addElement("<h1>Error - job id param not given.</h1>");
        else {
            log.debug("doRender() - 'job' param given looking for job.");

            try {
                Long jobIdLong = null;
                if (jobId != null) {
                    jobIdLong = new Long(jobId);
                }
                job = RecruitServiceLocator.getJobProvider().findJob(jobIdLong);
            } catch (Exception ex) {
                job = null;
                log.error("Exception finding job object from job id = " + jobId, ex);
            }

            if (job == null) {
                log.error("doRender() - could not find job param: " + jobId);
            } else {
                // Cell rightCell = pageRow.addCell();

                log.debug("doRender() - found job.  Displaying general job information");

                html.append(makeHistoryTable(job));
            }
        }

        log.debug("doRender() - done.");
        return html.toString();
    }

    protected Table makeHistoryTable(final Job job) {
        log.debug("makeHistoryTable() called.");
        BorderTable table = new BorderTable();
        table.setWidth(width);

        Row row = table.addRow();
        row.setDefaultBgImage(getContextPath() + "/site-graphics/tbl_hdr.gif");

        Cell cell = row.addCell();
        // cell.setClass("tableHdrBg");
        cell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("prompt.history.date")));
        cell = row.addCell();
        // cell.setClass("tableHdrBg");
        cell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("prompt.history.user")));
        cell = row.addCell();
        // cell.setClass("tableHdrBg");
        cell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("prompt.history.activity")));
        cell = row.addCell();
        // cell.setClass("tableHdrBg");
        cell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("prompt.history.description")));

        Iterator iter = HistoryProvider.getInstance().findObjectHistoryEntries(Job.OBJECT_TYPE, job.getId().toString())
                .iterator();

        while (iter.hasNext()) {
            Activity history = (Activity) iter.next();
            row = table.addRow();
            cell = row.addCell();
            cell.addElement(TextHelper.getFont("subFont", historyDateFormat.format(history.getDate())));
            cell = row.addCell();
            cell.addElement(TextHelper.getFont("subFont", history.getPerson().getName()));
            cell = row.addCell();
            cell.addElement(TextHelper.getFont("subFont", history.getActivityType().getDescription()));
            cell = row.addCell();
            cell.addElement(TextHelper.getFont("subFont", history.getDescription()));
        }

        return table;
    }

}
